package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author chenlida
 * @date 2019/1/14 11:51
 * @description
 */
public class Code382 {

}

class Solution {
    private int size;
    private List<Integer> list = new ArrayList<>();
    private Random random = new Random();

    /**
     * @param head The linked list's head.
     *             Note that the head is guaranteed to be not null, so it contains at least one node.
     */
    public Solution(ListNode head) {
        getSize(head);
    }

    /**
     * Returns a random node's value.
     */
    public int getRandom() {
        return list.get(random.nextInt(size));
    }

    private void getSize(ListNode node) {
        if (node != null) {
            size++;
            list.add(node.val);
            getSize(node.next);
        }

    }
}
